<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use app\api\model\News as news_list;
use app\api\model\Talent as talent_list;
class Talent extends Index{
    public function talent_index(){
        $number=(isset($_GET['number'])&&!empty($_GET['number']))?$_GET['number']:10;
        $page=(isset($_GET['page'])&&!empty($_GET['page']))?$_GET['page']:1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']): null;
        $work = (isset($_GET['work']) && $_GET['work']) ? trim($_GET['work']): null;
        $field = (isset($_GET['field']) && $_GET['field']) ? trim($_GET['field']): null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']): 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ?(strtotime($_GET['logmax']) + 24*60*60 -1): time();
        $status=(isset($_GET['status']) && $_GET['status']) ?$_GET['status']:array('in','1,2');
        $types=(isset($_GET['talentrole']) && $_GET['talentrole']) ?$_GET['talentrole']:array('neq',0);
        $area=(isset($_GET['area']) && $_GET['area']) ?$_GET['area']:array('EGT',0);
        $sheet=(isset($_GET['sheet']) && $_GET['sheet']) ?$_GET['sheet']:'';
        if(empty($sheet)){
            $this->_jsonp('1001','列表数据获取失败',null);
        }//sheet为1代表人才库，为2代表师资库
        $map['w.talentrole'] = $types;
        $map['w.talentname'] = array('like', "%$search%");
        $map['w.workplace'] = array('like', "%$work%");
        $map['w.field'] = array('like', "%$field%");
        $map['w.area'] = $area;
        $map['w.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $map['w.status'] = $status;
        $map['w.sheet'] = $sheet;
        $alias = 'w';
        $talent_list = new talent_list('talent');
        $count = $talent_list->count($map, $alias);
        $type1 = '__PROVINCE__ p';
        $type2 = 'zgh_talent_type t';
        $condition1 = 'w.area=p.id';
        $condition2 = 'w.talentrole=t.id';
        $fields = 'w.*,u.username,p.province,t.rolename';
        $data = $talent_list->talent_list($type1, $type2, $map, $number, $page, $condition1, $condition2, $fields);
        //dump($count);dump($data);
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = "";
        //分页
        if ($count !== 0) {
            $Page = new \app\api\model\Page($count, $number);
            $render = $Page->render;
            $res['render'] = $render;
        }
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '人才列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '人才列表数据获取失败', null);
        }
    }

    /**
     * 添加人才
     */
    public function talent_add(){
        if(!empty($_FILES)){
            $files=request()->file('img');
            $news_list=new news_list();
            $img=$news_list->upload($files,'Images/Talent');
            if(!$img){
                $this->json('1000','图片上传失败',null);
            }
            $data['img_url']=$img;
        }else{
            $data['img_url']='/head01.png';
        }
        $data['talentname']=htmlspecialchars(trim(input('post.talentname')));
        $data['sex']=input('post.sex');
        $data['label']=input('post.label');
        $data['duties']=htmlspecialchars(trim(input('post.duties')));
        $data['field']=htmlspecialchars(trim(input('post.field')));
        $data['workplace']=htmlspecialchars(trim(input('post.workplace')));
        $data['office']=htmlspecialchars(trim(input('post.office')));
        $data['composition']=htmlspecialchars(trim(input('post.composition')));
        $data['talentrole']=input('post.talentrole');
        $data['time']=time();
        $data['sheet']=input('post.sheet');
        $data['uid']=input('post.uid');
        $data['status']=input('post.status');
        $data['area']=input('post.area');
        //echo json_encode($data);die;

        $talent_list=new talent_list('talent');
        $res= $talent_list->talent_add($data);
        if(isset($res) && !empty($res)){
            action_log('添加人才成功', 'log_admin', 1);
            $this->json('200','添加成功',$res);
        }else{
            action_log('添加人才失败', 'log_admin');
            $this->json('1000','添加失败',null);
        }
    }

    /*验证是否重复*/
    public function talentname()
    {
        $where['talentname'] = input('post.talentname');
        $where['status'] = array('neq', 3);
        $news_list = new news_list('talent');
        $data = $news_list->check_name($where, "*");
        if (!$data) {
            echo json_encode(true);
        } else {
            echo json_encode(false);
        }
    }

    public function talent_detail()
    {
        $where['w.id'] = input('get.id');
        $type1 = '__PROVINCE__ p';
        $type2 = 'zgh_talent_type t';
        $condition1 = 'w.area=p.id';
        $condition2 = 'w.talentrole=t.id';
        $fields = 'w.*,u.username,p.province,t.rolename';
        $talent_list = new talent_list('talent');
        $res = $talent_list->talent_detail($type1, $type2, $where, $condition1, $condition2, $fields);
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', 'talent查询成功', $res);
        } else {
            $this->_jsonp('1000', 'talent', null);
        }
    }

    public function talent_edit()
    {
        $news_list = new news_list();
        $arr = array();
        if (!empty($_FILES)) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/Talent');
            $arr['img_url'] = $img;
        }
        $where['id'] = input('post.id');
        $data['talentname'] = htmlspecialchars(trim(input('post.talentname')));
        $data['sex'] = input('post.sex');
        $data['label'] = input('post.label');
        $data['duties'] = htmlspecialchars(trim(input('post.duties')));
        $data['field'] = htmlspecialchars(trim(input('post.field')));
        $data['workplace'] = htmlspecialchars(trim(input('post.workplace')));
        $data['office'] = htmlspecialchars(trim(input('post.office')));
        $data['composition'] = htmlspecialchars(trim(input('post.composition')));
        $data['talentrole'] = input('post.talentrole');
        //$data['time']=time();
        //$data['sheet']=input('post.sheet');
        $data['uid'] = input('post.uid');
        $data['status'] = input('post.status');
        $data['area'] = input('post.area');
        $result = array_merge($data, $arr);
        $talent_list = new talent_list('talent');
        $res = $talent_list->talent_edit($result, $where);
        if (isset($res) && !empty($res)) {
            action_log('人才库和师资库编辑', 'log_admin',1);
            $this->json('200', '修改成功', $res);
        } else {
            action_log('人才库和师资库编辑', 'log_admin');
            $this->json('1000', '修改失败', null);
        }
    }

    public function talent_del()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('talent');
            $data = $news_list->news_del($id, $data);
            if ($data['res']) {
                action_log('人才库和师资库修改状态', 'log_admin',1);
                $this->_jsonp('200', $data['name'] . '成功', null);
            } else {
                action_log('人才库和师资库修改状态', 'log_admin');
                $this->_jsonp('1000', $data['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    public function teacher_index()
    {

    }

    public function province()
    {
        $res['provice'] = Db::name('province')->select();
//        $where['class']=input('get.sheet');
//        $res['type']=Db::name('talent_type')->where($where)->select();
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    public function type()
    {
        $where['class'] = input('get.sheet');
        $res = Db::name('talent_type')->where($where)->select();
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '类型查询成功', $res);
        } else {
            $this->_jsonp('1000', '类型查询失败', null);
        }
    }

    /*
    * 前台人才培训展示师资与人才列表
    public function index()
    {
        $sql = "SELECT w.*,t.rolename FROM zgh_talent w LEFT JOIN zgh_talent_type t ON w.talentrole=t.id WHERE (SELECT count(*) FROM (SELECT *from zgh_talent WHERE status=1) b WHERE b.sheet=w.sheet AND b.time>w.time)<6 AND w.status=1 ORDER BY time desc";
        $res = Db::query($sql);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }*/
	/*
    * 前台人才培训展示师资与人才列表*/
    public function index()
    {
		$sheet=input('get.sheet');
        $res=Db::name('talent')->alias('w')
			->join('zgh_talent_type t','w.talentrole=t.id')
			->where('sheet',$sheet)->where('status',1)->field('w.*,t.rolename')->order('w.time desc')->limit(6)->select();
			
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /*
     * 推荐人才
     * */
    public function random_talent()
    {
        $talent = new talent_list('talent');
        $map['w.status'] = 1;
        $map['w.sheet'] = (isset($_GET['sheet']) && $_GET['sheet']) ? $_GET['sheet'] : array('neq', 0);
        $limit = 4;
        $type1 = '__PROVINCE__ p';
        $type2 = 'zgh_talent_type t';
        $condition1 = 'w.area=p.id';
        $condition2 = 'w.talentrole=t.id';
        $fields = 'w.id,w.talentname,w.img_url,w.field,u.username,p.province,t.rolename';
        $data = $talent->talent_list($type1, $type2, $map, $limit, 1, $condition1, $condition2, $fields, 'rand()');
        if (!empty($data)) {
            $this->_jsonp('200', '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }
   

}